package com.yumeng.framework.auth.security.authentication.custom;

import com.yumeng.common.data.enums.AuthClientType;
import com.yumeng.common.data.enums.AuthMode;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import java.util.Objects;

/**
 * @author wxd
 * @date 2025/3/4 16:17
 */
public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {

    public static final String REQUEST_KEY_AUTH_CLIENT_TYPE = "yumeng_CustomWebAuthenticationDetails_AuthClientType";


    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    private AuthClientType authClientType;

    public CustomWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        this.authClientType = (AuthClientType) request.getAttribute(REQUEST_KEY_AUTH_CLIENT_TYPE);
    }

    public CustomWebAuthenticationDetails(String remoteAddress, String sessionId, AuthMode authMode, AuthClientType authClientType) {
        super(remoteAddress, sessionId);
        this.authClientType = authClientType;
    }

    public AuthClientType getAuthClientType() {
        return authClientType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        CustomWebAuthenticationDetails that = (CustomWebAuthenticationDetails) o;
        return Objects.equals(super.getRemoteAddress(), that.getRemoteAddress())
                && Objects.equals(super.getSessionId(), that.getSessionId())
                && Objects.equals(this.authClientType, that.authClientType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.authClientType, super.getRemoteAddress(), super.getSessionId());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName()).append(" [");
        sb.append("AuthClientType=").append(this.authClientType).append(", ");
        sb.append("RemoteIpAddress=").append(super.getRemoteAddress()).append(", ");
        sb.append("SessionId=").append(super.getSessionId()).append("]");
        return sb.toString();
    }
}
